package com.snow.useviewdemo.function.calendar;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-11-24
 * desc   :日历View
 */
public class CalendarView extends FrameLayout implements AdapterCalendarItem.CalendarItemClickListener {
    private Context mContext;
    RecyclerView recyclerView;
    private int totalHeight;
    private int totalWidth;
    private AdapterCalendarItem calendarAdapter;
    private List<CalendarData> calendarDataList;
    private CalendarClickListener clickLinstener;
    //从服务器获取的今天时间2019-12-12 Android本地时间可以修改
    private String todayDate;

    public CalendarView(@NonNull Context context) {
        super(context);
    }

    public CalendarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        calendarDataList = new ArrayList<>();
        initView();
    }

    private void initView() {
        recyclerView = new RecyclerView(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        recyclerView.setLayoutParams(layoutParams);
        addView(recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 7));
        calendarAdapter = new AdapterCalendarItem(mContext, this);

        recyclerView.setAdapter(calendarAdapter);
    }

    /**
     * 日历点击监听
     *
     * @param clickLinstener
     */
    public void setCalendarClickListener(CalendarClickListener clickLinstener) {
        this.clickLinstener = clickLinstener;
    }

    /**
     * 设置当前日期
     */
    public void setTodayDate(String todayDate) {
        this.todayDate = todayDate;
        calendarAdapter.setTodayPosition(todayDate);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        totalHeight = getMeasuredHeight();
        totalWidth = getMeasuredWidth();
//        Log.e("snow_m", "=========" + totalHeight);
    }

    /**
     * 重新设置日历数据
     *
     * @param year
     * @param month
     */
    public void resetCalendarData(int year, int month, List<ClockInBean> clockInList) {
        calendarDataList.clear();
        if (clockInList == null) {
            clockInList = new ArrayList<>();
        }
//        clockInList.add(new ClockInBean("2019-11-01", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-08", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-09", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-10", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-11", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-12", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-13", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-14", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-15", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-16", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-24", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-25", "的等到"));
//        clockInList.add(new ClockInBean("2019-11-29", "的等到"));
        int monthLastDay = CalendarUtils.init().getMonthLastDay(year, month);
        int dayOfWeek = CalendarUtils.init().getFirstDayOfWeek(year, month);
        int dayOfMonth = CalendarUtils.init().getTodayOfMonth();
        //当前页面显示上月的数据，或者空
        for (int i = 0; i < dayOfWeek; i++) {
            CalendarData calendarData = new CalendarData();
            calendarData.setSolarNum("");
            calendarDataList.add(calendarData);
        }
        //设置当月数据
        for (int i = 0; i < monthLastDay; i++) {
            CalendarData calendarData = new CalendarData();
            calendarData.setYearMD(year + "-" + month + "-" + CalendarUtils.init().addZeroBefore(i + 1));
            calendarData.setSolarNum(String.valueOf(i + 1));
            calendarDataList.add(calendarData);
        }
//        calendarAdapter.setTodayPosition(dayOfMonth + dayOfWeek, CalendarUtils.init().getTodayMonth());
        //计算共几行
        int rowNum = (int) Math.ceil((monthLastDay + dayOfWeek) / 7f);
        calendarAdapter.setItemWidthHeight(totalHeight / rowNum, rowNum, totalWidth / 7);

        CalendarUtils.init().addClockInInfoToCalendarInfo(year, month, dayOfWeek, calendarDataList, clockInList);

        calendarAdapter.setDataList(calendarDataList);
    }

    public void setCalendarDataList(int year, int month, List<CalendarData> calendarDataList) {
        int monthLastDay = CalendarUtils.init().getMonthLastDay(year, month);
        int dayOfWeek = CalendarUtils.init().getFirstDayOfWeek(year, month);
        //计算共几行
        int rowNum = (int) Math.ceil((monthLastDay + dayOfWeek) / 7f);
        calendarAdapter.setItemWidthHeight(totalHeight / rowNum, rowNum, totalWidth / 7);

//        CalendarUtils.init().addClockInInfoToCalendarInfo(year, month, dayOfWeek, calendarDataList, clockInList);

        calendarAdapter.setDataList(calendarDataList);
    }

    @Override
    public void itemClick(CalendarData data, int position) {
//        calendarDataList.get(position)
        if (TextUtils.isEmpty(data.getSolarNum())) {
            return;
        }
        if (clickLinstener != null) {
            clickLinstener.clickItem(data);
        }
        currentClickDate = data.getYearMD();
        calendarAdapter.setCurrentClickDate(currentClickDate);
    }

    private String currentClickDate;

    /**
     * 获取当前点击的日期
     *
     * @return
     */
    public String currentClickDate() {
        return currentClickDate;
    }
}
